import React from "react";
import { DataAction, IProTableRef } from "vendor/components/pro-table-native";
import { getDictionaryDataPageList, getDictionaryDataInfo, createDictionaryData, updateDictionaryData, deleteDictionaryData } from "../api";

export const getDataAction = (proTableRef: React.MutableRefObject<IProTableRef>, dictType: string): DataAction => ({
    fetchList: (params) => {
        const { page, pageSize, filter } = params;
        return new Promise((resolve, reject) => getDictionaryDataPageList(Object.assign({page,pageSize,dictType}, filter || {})).then((resp) => resolve(resp.data)).catch((e) => reject(e)))
    },
    getItem: (rowKey) => {
        const dataSetId = rowKey as number;
        return new Promise((resolve, reject) => getDictionaryDataInfo(dataSetId).then((resp) => {
            resolve({
                dictLabel: resp.data.dictLabel,
                dictValue: resp.data.dictValue,
                dictSort: resp.data.dictSort,
                status: resp.data.status,
                remark: resp.data.remark,
            });
        }).catch((e) => reject(e)));
    },
    createItem: (params) => {
        return new Promise((resolve, reject) => createDictionaryData({...params, dictType}).then((resp) => {
            proTableRef.current.doFetchList();
            resolve(resp.data)
        }).catch((e) => reject(e)))
    },
    updateItem: (rowKey, params) => {
        return new Promise((resolve, reject) => updateDictionaryData(rowKey as number, {...params,dictType}).then((resp) => {
            proTableRef.current.doFetchList();
            resolve(resp.data)
        }).catch((e) => reject(e)))
    },
    deleteItems: (selectedRowKeys) => {
        return deleteDictionaryData(selectedRowKeys.map((item) => parseInt(item)))
    },
})


